home *** CD-ROM | disk | FTP | other *** search
- /* */
- /* cmerge - merge an assembly language and C file */
- /* */
- /* replace ; Line <number> lines in the assembly output with */
- /* lines from the original C source as comments */
- /* */
-
- #include <stdio.h>
-
-
- #define MAXLINE 256 /* maximum line size */
- #define MAXFNAME 80 /* maximum filename size */
-
- #define errno 1
-
- char *lst_ptr; /* ptr to assy line (LST) file */
- char *ext_ptr; /* extension pointer */
- char aline[MAXLINE]; /* assembly line */
- char cline[MAXLINE]; /* C source line */
- char opt_ar[MAXFNAME]; /* array for option name */
- char out_ar[MAXFNAME]; /* output filename */
- char inp_ar[MAXFNAME]; /* input assembly filename */
- char pagestr[MAXLINE];
- char mypagestr[MAXLINE];
- char pagefound = 0;
- int output_code; /* output file code */
- int input_code; /* input file code */
- int opt_cnt; /* input line option count/index */
- unsigned clinenumber=0; /* C line counter */
- unsigned pageno=0;
- unsigned linecnt=0;
-
- FILE *cfp,*afp; /* C, assembly FILE pointer */
- FILE *ofp; /* output file pointer */
-
- char tolower(char);
-
- main(argc,argv)
- int argc;
- char **argv;
-
- {
- unsigned alinenumber; /* assy line counter */
- opt_cnt = 2; /* initialize option count/index */
- output_code = 0; /* output code = default (stdout) */
- input_code = 0; /* input code=default (.LST file) */
-
- if(argc < 2 || argc > 4) /* improper input line count */
- { /* terminate program */
- fprintf(stderr,"cmerge: usage cmerge <name> [-a] [-a=<file>] [-o] [-o=<file>]\n");
- exit(1);
- }
-
- /* --------------------- decode options ------------------------------------ */
-
- while((argc-1) >= opt_cnt) /* if options exist, decode */
- {
- strcpy(opt_ar, argv[opt_cnt]); /* copy option into array */
-
- if(opt_ar[0] != '-') /* '-' does not precede opt. */
- {
- fprintf(stderr,"cmerge: illegal option syntax;\n");
- fprintf(stderr,"\t[-a] [-a=<file>] [-o] [-o=<file>]\n");
- exit(errno);
- }
-
- if(tolower(opt_ar[1]) == 'a') /* 'a' option-redirect input */
- {
- if(dec_input() == 0) /* decode input option */
- {
- exit(errno);
- }
- }
-
- else if(tolower(opt_ar[1]) == 'o') /* 'o' option-redirect output*/
- {
- if(dec_output() == 0) /* decode output option */
- {
- exit(errno);
- }
- }
-
- else /* illegal option, terminate*/
- {
- fprintf(stderr,"cmerge: '-%c' undefined option",opt_ar[1]);
- fprintf(stderr," [-a] [-o]\n");
- exit(errno);
- }
-
- ++opt_cnt; /* inc option index */
- }
- /* ------------------------------------------------------------------------- */
-
- if(set_io(argv) == 0) /* set redirection (i/o) */
- {
- exit(errno);
- }
-
- strcpy(cline, argv[1]); /* set C input filename */
- strcat(cline, ".c");
-
- if(output_code != 0) /* open output file */
- {
- if((ofp = fopen(out_ar, "w")) == NULL)
- {
- fprintf(stderr,"cmerge: error opening output file ");
- fprintf(stderr,"%s\n", out_ar);
- exit(errno);
- }
- }
- else /* output code = 0 */
- {
- ofp = stdout; /* set output = stdout */
- }
-
- if((cfp=fopen(cline,"r")) == NULL) /* open C source file */
- {
- fprintf(stderr,"cmerge: can't open %s\n",cline);
- exit(errno);
- }
-
- if((afp=fopen(aline,"r")) == NULL) /* open assembly sorurce file*/
- {
- fprintf(stderr,"cmerge: can't open %s\n",aline);
- exit(errno);
- }
-
- /* --------------------- output -------------------------------------------- */
-
- while(fgets(aline,MAXLINE,afp) != NULL) /* output assy/C source files*/
- {
- lst_ptr = aline; /* set ptr to aline */
- while(isspace(*lst_ptr)) /* skip lead in spaces */
- {
- lst_ptr++;
- }
-
- if(strncmp(lst_ptr,"; Line ",7) == 0) /*search for C source*/
- { /* insert flagNETWORK/
- /* insert C source @ flag */
- *lst_ptr = '\0'; /* insert delimiter */
- alinenumber=atoi(&lst_ptr[7]); /*C source line count*/
- while(clinenumber < alinenumber) /* insert C source */
-
- {
- strcpy(cline,";|*** ");
- if(fgets(cline+6,MAXLINE-2,cfp) != NULL)
- {
- fputs(cline, ofp); /* insert C src. line*/
- checkpage();
- }
- ++clinenumber; /* inc C line # */
- }
- fprintf(ofp,"; Line %d\n",alinenumber);
- checkpage();
- }
- else
- {
- if(strncmp(lst_ptr+1,"Microsoft",9) == 0)
- continue;
- else
- if (strstr(aline,"Page"))
- {
- pagefound = 1;
- strcpy(mypagestr,aline);
- }
- else
- {
- fputs(aline, ofp); /* output assy.text */
- checkpage();
- }
- }
- }
-
- /* output remaining C stuff */
-
- while(fgets(cline,MAXLINE,cfp) != NULL)
- {
- fputs(cline, ofp); /* output C src. line*/
- checkpage();
- }
- }
-
- #include <time.h>
-
- checkpage()
- {
- long secs_now;
- char *str_now;
-
-
- if (pagefound)
- if ((linecnt++ % 60) == 0)
- {
- time(&secs_now);
- str_now = ctime(&secs_now);
- pageno++;
- itoa(pageno,pagestr,10);
- strncpy(mypagestr,"Cmerge V1.0",strlen("Cmerge V1.0"));
- strncpy(mypagestr+strlen("Cmerge V1.0")+5,str_now,strlen(str_now)-1);
- strcpy(strchr(mypagestr,'-')+1,pagestr);
- fputs("\014",ofp);
- fputs(mypagestr,ofp);
- fputs("\n\n",ofp);
- ++linecnt;
- }
- }
-
- /* ------------------------------------------------------------------------- */
- /* - dec_input(); decode input options - */
- /* ------------------------------------------------------------------------- */
-
- int dec_input()
-
- {
- if(input_code != 0) /* input already redirected */
- {
- fprintf(stderr,"cmerge: conflicting input redirection\n");
- return(0); /* return w/ error */
- }
-
- if(opt_ar[2] == '\0') /* input from name.ASM */
- {
- input_code = 1; /* set input code */
- }
-
- else if(opt_ar[2] == '=') /* input from redifined name */
- {
- strcpy(inp_ar, &opt_ar[3]); /* copy f-name into input ar */
- if((strchr(inp_ar, '.')) == 0) /* is ext. specified */
- {
- input_code = 2; /* f-name w/o extension */
- }
- else
- {
- input_code = 3; /* f-name w/ extension */
- }
- }
-
- else /* unrecognizable option */
- {
- fprintf(stderr,"cmerge: syntax error; input option\n");
- return (0); /* return w/ error */
- }
-
- return(1); /* return w/o error */
- }
-
- /* ------------------------------------------------------------------------- */
- /* - dec_output(); decode output options - */
- /* ------------------------------------------------------------------------- */
-
- int dec_output()
-
- {
- if (output_code != 0) /* output already redirected */
- {
- fprintf(stderr,"cmerge: conflicting output redirection\n");
- return (0); /* return w/ error */
- }
-
- if(opt_ar[2] == '\0') /*output to f-name w/CLS ext */
- {
- output_code = 1; /* set output code */
- }
- else if(opt_ar[2] == '=') /* set output to new filename*/
- {
- strcpy(out_ar, &opt_ar[3]); /* copy filename into out_ar */
- if(strchr(out_ar, '.') == 0) /* search for extension */
- {
- output_code = 2; /* no extension specified */
- }
- else
- {
- output_code = 3; /*extension specified */
- }
- }
-
- else /* unrecognizable option */
- {
- fprintf(stderr,"cmerge: syntax error; output option\n");
- return (0); /* return w/o error */
- }
-
- return (1); /* return w/o error */
- }
-
- /* ------------------------------------------------------------------------- */
- /* - set_io (); set the assembly file input and file output - */
- /* ------------------------------------------------------------------------- */
-
- int set_io (argv)
-
- char **argv;
- {
-
- /***** set assembly input *****/
-
- if(input_code == 0) /* default input, "name.LST" */
- {
- strcpy(aline, argv[1]); /* create assembly filename */
- strcat(aline, ".LST");
- }
-
- else if(input_code == 1) /* input; "name.ASM" */
- {
- strcpy(aline, argv[1]); /* create assembly filename */
- strcat(aline, ".asm");
- }
-
- else if(input_code == 2) /* input; specified name. */
- {
- strcpy(aline, inp_ar); /* create assembly filename */
- strcat(aline, ".LST");
- }
-
- else if(input_code == 3) /* input; f-name.ext */
- {
- strcpy(aline, inp_ar); /* create assembly filename */
- }
-
- else /* error */
- {
- fprintf(stderr,"cmerge: error redirecting input;\n");
- fprintf(stderr," undefined code\n");
- return (0); /* return w/ error */
- }
-
- /***** set file output *****/
-
- if(output_code == 0) /* default output = stdout */
- {
- ofp = stdout; /* set std output */
- }
-
- else if(output_code == 1) /* output; assy. "name" */
- {
- strcpy(out_ar, argv[1]); /* create output filename */
- if((ext_ptr = (strchr(aline, '.'))) == 0) /* find ext */
- {
- fprintf(stderr,"cmerge: error redirecting output;\n");
- fprintf(stderr," file extension not found\n");
- return (0); /* return w/ error */
- }
-
- else /* decode ext */
- {
- if(strncmp(ext_ptr, ".ASM", 4) == 0)
- {
- strcat(out_ar, ".asl"); /* set ext. = asl */
- }
-
- else
- {
- strcat(out_ar, ".lsl"); /* set ext. = lsl */
- }
- }
- }
-
- else if(output_code == 2) /* output; f-name.x */
- { /* x = assy. extension */
- if((ext_ptr =(strchr(aline, '.'))) == 0) /* search for assy. ext */
- {
- fprintf(stderr,"cmerge: error redirecting output;\n");
- fprintf(stderr," file extension not found\n");
- return (0); /* return w/ error */
- }
-
- else
- {
- if(strncmp(ext_ptr,".asm", 4) == 0) /* if assy = .ASM */
- {
- strcat(out_ar, ".asl"); /* output will be .asl ext. */
- }
- else
- {
- strcat(out_ar, ".lsl"); /* output will be .lsl ext. */
- }
- }
- }
-
- else if((output_code != 0) && (output_code != 3)) /* not default =0 */
- { /* not specif. =3 */
- fprintf(stderr,"cmerge: error redirecting output;\n");
- fprintf(stderr," undefined code\n");
- return (0); /* return w/ error */
- }
-
- return (1); /* return w/o error */
- }
-
- /* */
- /* tolower - convert the given character to lower case */
- /* */
-
- char tolower(ch)
- char ch;
- {
- return isupper(ch) ? ch+('a'-'A') : ch;
- }
-
- /* */
- /* isupper - return TRUE if the given character if uppercase */
- /* */
-
- isupper(ch)
- char ch;
- {
- return ch >= 'A' && ch <= 'Z';
- }
-
- /* */
- /* isspace - return TRUE if the given character is a whitespace */
- /* */
-
- isspace(ch)
- char ch;
- {
- return ch == ' ' || ch == '\n' || ch == '\t';
- }
-